<?php
namespace app\question\controller\admin;
use app\common\controller\Admin;
use app\common\annotation\NodeAnnotation;
use app\common\traits\ControllerCurd;
use app\question\model\QuestionExam;
use app\question\model\QuestionSubject;
use think\Request;

/**
 * @NodeAnnotation(
 *     title="考试管理",
 *     parent="question/admin.index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1,
 * )
 */
class Questexam extends Admin
{
    use ControllerCurd;
    public $model;
    public function initialize()
    {
        parent::initialize();
        $this->model = new QuestionExam();
    }

    /**
     * @NodeAnnotation(
     *     title="考试列表",
     *     parent="question/admin.questexam",
     *     MenuRoot="question/admin.index"
     * )
     */
    public function index()
    {
        $this->assign([
            "examList" => $this->model->field('id,title,status,start_time,end_time')->paginate()
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="添加科目",
     *     parent="question/admin.questexam",
     *     MenuRoot="question/admin.index",
     *     is_menu = 0
     * )
     */
    public function addexam(Request $request)
    {
        if($request->isAjax())
        {
            $input = $request->post();
            if(isset($input['datetime']))
            {
                $input['datetime']   = explode("~",$input['datetime']);
                $input['start_time'] = strtotime($input['datetime'][0]);
                $input['end_time']   = strtotime($input['datetime'][1]);
                unset($input['datetime']);
            }
            $res = $this->model->create($input);
            if($res)
            {
                $this->success("添加成功",url('question/admin.questexam/index'));
            }else{
                $this->error("添加失败");
            }
        }
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑科目",
     *     parent="question/admin.questexam",
     *     MenuRoot="question/admin.index",
     *     is_menu = 0
     * )
     */
    public function editexam(Request $request,$id=0)
    {
        $this->assign([
            "exam" =>  $this->model->findData($id),
        ]);
        if($request->isAjax())
        {
            $input = $request->post();
            if(isset($input['datetime']))
            {
                $input['datetime']   = explode("~",$input['datetime']);
                $input['start_time'] = strtotime($input['datetime'][0]);
                $input['end_time']   = strtotime($input['datetime'][1]);
                unset($input['datetime']);
            }
            $res = $this->model->update($input);
            if($res)
            {
                $this->success("修改成功",url('question/admin.questexam/index'));
            }else{
                $this->error("修改失败");
            }
        }
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="删除科目",
     *     parent="question/admin.questexam",
     *     MenuRoot="question/admin.index",
     *     is_menu = 0
     * )
     */
    public function delexam()
    {
        $this->del();
    }
}